//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ZeroCouponBond.h"
using namespace Cephei::QL::Instruments::Bonds;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/Bond.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CBond(CZeroCouponBond::typeid)
{
    CCalendar^ _Ccalendar;
    try
    {
#ifdef HANDLE
        _phZeroCouponBond = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Real _faceAmount = (QuantLib::Real)ValueHelper::Convert (faceAmount);
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate);
        QuantLib::BusinessDayConvention _paymentConvention = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>::IsSome::get (paymentConvention) ? (QuantLib::BusinessDayConvention)paymentConvention->Value : QuantLib::BusinessDayConvention::Following); //10
        QuantLib::Real _redemption = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (redemption) ? (QuantLib::Real)ValueHelper::Convert (redemption->Value) : 100.0); //4
        QuantLib::Date _issueDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (issueDate) ? (QuantLib::Date)ValueHelper::Convert (issueDate->Value) : QuantLib::Date()); //4
        _ppZeroCouponBond = new boost::shared_ptr<QuantLib::ZeroCouponBond> (new QuantLib::ZeroCouponBond ( _settlementDays,  _calendar,  _faceAmount,  _maturityDate,  _paymentConvention,  _redemption,  _issueDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppZeroCouponBond)->setPricingEngine (_QL_Pricer);
        SetBond (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
    }
}
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (boost::shared_ptr<QuantLib::ZeroCouponBond>& childNative, Object^ owner) : CBond(CZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phZeroCouponBond = NULL;
#endif
	_ppZeroCouponBond = &childNative;
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
}
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (QuantLib::ZeroCouponBond& childNative, Object^ owner) : CBond(CZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phZeroCouponBond = NULL;
#endif
	_ppZeroCouponBond = new boost::shared_ptr<QuantLib::ZeroCouponBond> (&childNative);
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
    _ZeroCouponBondOwner = owner;
    _BondOwner = owner;
}

Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (CZeroCouponBond^ copy) : CBond(CZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phZeroCouponBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppZeroCouponBond = new boost::shared_ptr<QuantLib::ZeroCouponBond> (copy->GetShared());
        _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
    }
}
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (System::Type^ t) : CBond(CZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phZeroCouponBond = NULL;
#endif
	if (!t->IsSubclassOf(CZeroCouponBond::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (QuantLib::Handle<QuantLib::ZeroCouponBond>& childNative, Object^ owner)  : CBond(CZeroCouponBond::typeid)
{
	_phZeroCouponBond = &childNative;
	_ppZeroCouponBond = &static_cast<boost::shared_ptr<QuantLib::ZeroCouponBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
    _ZeroCouponBondOwner = owner;
}
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (QuantLib::Handle<QuantLib::ZeroCouponBond> childNative)  : CBond(CZeroCouponBond::typeid)
{
	_phZeroCouponBond = &childNative;
	_ppZeroCouponBond = &static_cast<boost::shared_ptr<QuantLib::ZeroCouponBond>>(childNative.currentLink());
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::Bonds::CZeroCouponBond::CZeroCouponBond (QuantLib::ZeroCouponBond childNative)  : CBond(CZeroCouponBond::typeid)
{
#ifdef HANDLE
	_phZeroCouponBond = NULL;
#endif
	_ppZeroCouponBond = new boost::shared_ptr<QuantLib::ZeroCouponBond> (new QuantLib::ZeroCouponBond (childNative));
    _ppBond = new boost::shared_ptr<QuantLib::Bond> (boost::dynamic_pointer_cast<QuantLib::Bond> (*_ppZeroCouponBond));
}
#endif

Cephei::QL::Instruments::Bonds::CZeroCouponBond::~CZeroCouponBond ()
{
    if (_ppZeroCouponBond != NULL)
    {
	    delete _ppZeroCouponBond;
        _ppZeroCouponBond = NULL;
    }
}
Cephei::QL::Instruments::Bonds::CZeroCouponBond::!CZeroCouponBond ()
{
    if (_ppZeroCouponBond != NULL)
    {
	    delete _ppZeroCouponBond;
    }
}
QuantLib::ZeroCouponBond& Cephei::QL::Instruments::Bonds::CZeroCouponBond::GetReference ()
{
    if (_ppZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return **_ppZeroCouponBond;
}
boost::shared_ptr<QuantLib::ZeroCouponBond>& Cephei::QL::Instruments::Bonds::CZeroCouponBond::GetShared ()
{
    if (_ppZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return *_ppZeroCouponBond;
}
QuantLib::ZeroCouponBond* Cephei::QL::Instruments::Bonds::CZeroCouponBond::GetPointer ()
{
    if (_ppZeroCouponBond == NULL) throw gcnew NativeNullException ();
	return &**_ppZeroCouponBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ZeroCouponBond>& Cephei::QL::Instruments::Bonds::CZeroCouponBond::GetHandle ()
{
	if (_phZeroCouponBond == NULL)
	{
		_phZeroCouponBond = new Handle<QuantLib::ZeroCouponBond> (*_ppZeroCouponBond);
	}
	return *_phZeroCouponBond;
}
#endif
bool Cephei::QL::Instruments::Bonds::CZeroCouponBond::HasNative () 
{
	return (_ppZeroCouponBond != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::Bonds::IZeroCouponBond^ Cephei::QL::Instruments::Bonds::CZeroCouponBond_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Double faceAmount, DateTime maturityDate, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ paymentConvention, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CZeroCouponBond ( settlementDays,  calendar,  faceAmount,  maturityDate,  paymentConvention,  redemption,  issueDate,  QL_Pricer);
}
